package dk.controller;

import static org.mockito.Mockito.RETURNS_DEEP_STUBS;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ReturnData.ResponseData;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;

import dk.beans.Datelist;
import dk.mapper.DateListMapper;

@RestController

public class DateListController {
	@Autowired
	DateListMapper datelistmapper;
@PostMapping("GetDateListByDate")
public ResponseData GetDateListByDate(
		String StartTimeYear,
		String StartTimeMonth,
		String StartTimeDay,
		String  EndTimeYear,
		String EndTimeMonth,
		String EndTimeDay
		
		) throws ParseException {
	ResponseData data = new ResponseData();
	SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
	//发过来请求格式化 这是开始时间
	String StartTimestr = StartTimeYear+StartTimeMonth+StartTimeDay;
	String EndTimeStr = EndTimeYear+EndTimeMonth+EndTimeDay;
	Date EndTime = sdf1.parse(EndTimeStr);
	Date StartTime = sdf1.parse(StartTimestr);
	
	QueryWrapper<Datelist> qw = new QueryWrapper<Datelist>();
	qw.between("runtime", StartTime, EndTime);
	List<Datelist> selectList = datelistmapper.selectList(qw);	
	if(selectList.size() == 0) {
		data.setCode(201);
		data.setMessage("当前时间获取不到日程");
		return data;
	}
	data.setData(selectList);
	data.setMessage("获取指定时间段日程成功");
	data.setCode(200);
	return data;
	
}
@PostMapping("DeleteDateListById")
public ResponseData DeleteDateListById(Integer id) {
	ResponseData data = new ResponseData();
	datelistmapper.delete(new QueryWrapper<Datelist>().eq("id", id));
	data.setCode(200);
	data.setMessage("删除成功");
	return data;
}
@PostMapping("GetDateListById")
public ResponseData GetDateListById(Integer id) {
	ResponseData data = new ResponseData();
	Datelist datelist =  datelistmapper.selectOne(new QueryWrapper<Datelist>().eq("id", id));
	data.setCode(200);
	data.setMessage("根据id获取datelist信息成功");
	data.setData(datelist);
	return data;
}
@PostMapping("UpdateDateListById")
public ResponseData UpdateDateListById(String title,String content,Integer level,String runtime,Integer id) throws ParseException 
{   
	
	
	ResponseData data = new ResponseData();
	//应判断时间 是否为今天之前
	//获取到的时间
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.S");
	Date date = simpleDateFormat.parse(runtime);
	//现在的时间
	Date localdate = new Date();
	if(date.before(localdate)) {
		data.setCode(201);
		data.setMessage("修改失败,日期应不小于今天");
		return data;
	}
	Datelist datelist = new Datelist();
	datelist.setId(id);
	
	datelist.setContent(content);
	datelist.setTitle(title);
	datelist.setRuntime(date);
	datelist.setLevel(level);
	System.out.println(datelist);
	datelistmapper.update(datelist, new QueryWrapper<Datelist>().eq("id", datelist.getId()));
    data.setCode(200);
    data.setMessage("修改成功");
	
	return data;
	
	
	
	

}
@PostMapping("/InsertDateList")
public ResponseData InsertDateList (String title,String content,Integer level,String year,String month,String day) throws ParseException {
	
	ResponseData data = new ResponseData();
	//空值判断
	if(StringUtils.isEmpty(title)
	 ||StringUtils.isEmpty(content)
	 
	 ||level ==0
	) {
		data.setData(201);
		data.setMessage("您提交得字段含有空值,请检查");
		return data;
	}
	Datelist datelist = new Datelist();
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
	String runtime = year+month+day;
	Date date = simpleDateFormat.parse(runtime);
	Date localdate = new Date();
	if(date.before(localdate)) {
		data.setCode(201);
		data.setMessage("修改失败,日期应不小于今天");
		return data;
	}
	datelist.setContent(content);
	datelist.setTitle(title);
	datelist.setRuntime(date);
	datelist.setLevel(level);
	
	datelistmapper.insert(datelist);
	data.setCode(200);
	data.setMessage("添加成功");
	
	
	
	return data;
}

}
